package cn.jltx.abslistviewadapterdemo.ExpandableEdit;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import cn.jltx.abslistviewadapterdemo.R;
import cn.jltx.abslistviewadapterdemo.ViewHolderUtils;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/05/04.
 */

public class ExpandableEditAdapter extends BaseExpandableListAdapter{
    private Context context;
    private List<GroupBean> groupBeanList;
    private int groupIndex = -1;
    private int childIndex = -1;
    private int editId = -1;

    public ExpandableEditAdapter(Context context, List<GroupBean> groupBeanList) {
        this.context = context;
        this.groupBeanList = groupBeanList;
    }

    @Override
    public int getGroupCount() {
        if(groupBeanList != null){
            return groupBeanList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(groupBeanList != null && groupPosition >=0 && groupPosition < groupBeanList.size()){
            GroupBean groupBean = groupBeanList.get(groupPosition);
            if(groupBean != null){
                return groupBean.getChildList().size();
            }
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if(groupBeanList != null && groupPosition >=0 && groupPosition < groupBeanList.size()){
            return groupBeanList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if(groupBeanList != null && groupPosition >=0 && groupPosition < groupBeanList.size()){
            GroupBean groupBean = groupBeanList.get(groupPosition);
            if (groupBean != null) {
                List<ItemBeanEdit> itemBeanEditList = groupBean.getChildList();
                if(itemBeanEditList != null && childPosition >=0 && childPosition < itemBeanEditList.size()){
                    return itemBeanEditList.get(childPosition);
                }
            }
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView =  inflater.inflate(R.layout.group_layout,parent,false);
        }
        TextView textView = ViewHolderUtils.get(convertView,R.id.parent_tv);
        GroupBean groupBean = groupBeanList.get(groupPosition);
        if(groupBean != null){
            textView.setText(groupBean.getGroup() != null ? groupBean.getGroup() : "");
        }

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.child_edit_layout,parent,false);
        }
        TextView textView = ViewHolderUtils.get(convertView,R.id.child_tv);
        EditText editText = ViewHolderUtils.get(convertView,R.id.child_ed);
        EditText editText1 = ViewHolderUtils.get(convertView,R.id.child_ed1);
        GroupBean groupBean = groupBeanList.get(groupPosition);
        //先清除所有EditText的焦点
        editText.clearFocus();
        editText1.clearFocus();
        //清除所有EditText里面的文字输入监听器，通过Tag来缓存和提取
        if(editText.getTag() instanceof TextWatcher){
            editText.removeTextChangedListener((TextWatcher)editText.getTag());
        }
        //清除所有EditText里面的文字输入监听器，通过Tag来缓存和提取
        if(editText1.getTag() instanceof TextWatcher){
            editText1.removeTextChangedListener((TextWatcher)editText1.getTag());
        }

        if(groupBean != null && groupBean.getChildList() != null){
            ItemBeanEdit itemBeanEdit =  groupBean.getChildList().get(childPosition);
            textView.setText(itemBeanEdit.getiName() != null ? itemBeanEdit.getiName() : "");

            //设置Edittext上一次的内容
            editText.setText(itemBeanEdit.getEditStr() != null ? itemBeanEdit.getEditStr() : "");
            MyTextWatcher myTextWatcher = new MyTextWatcher(R.id.child_ed,itemBeanEdit);
            editText.addTextChangedListener(myTextWatcher);
            editText.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP){
                        groupIndex = groupPosition;
                        childIndex = childPosition;
                        editId = v.getId();
                    }
                    return false;
                }
            });
            //将内容监听器缓存到tag里面方面重新刷时
            editText.setTag(myTextWatcher);

            editText1.setText(itemBeanEdit.getEditStr1() != null ? itemBeanEdit.getEditStr1() : "");
            MyTextWatcher myTextWatcher1 = new MyTextWatcher(R.id.child_ed1,itemBeanEdit);
            editText1.addTextChangedListener(myTextWatcher1);
            editText1.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP){
                        groupIndex = groupPosition;
                        childIndex = childPosition;
                        editId = v.getId();
                    }
                    return false;
                }
            });
            editText1.setTag(myTextWatcher1);

            if(groupIndex == groupPosition && childIndex == childPosition){
                if(editId == R.id.child_ed){
                    editText.requestFocus();
                    editText.setSelection(itemBeanEdit.getEditStr() != null ? itemBeanEdit.getEditStr().length() : 0);
                }else if(editId == R.id.child_ed1){
                    editText1.requestFocus();
                    editText1.setSelection(itemBeanEdit.getEditStr1() != null ? itemBeanEdit.getEditStr1().length() : 0);
                }

            }

        }
        return convertView;
    }

    static class MyTextWatcher implements TextWatcher{
        private int viewId;
        private ItemBeanEdit itemBeanEdit;

        public MyTextWatcher(int viewId, ItemBeanEdit itemBeanEdit) {
            this.viewId = viewId;
            this.itemBeanEdit = itemBeanEdit;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if(viewId == R.id.child_ed){
                itemBeanEdit.setEditStr(s.toString());
            }else if(viewId == R.id.child_ed1){
                itemBeanEdit.setEditStr1(s.toString());
            }

        }
    }
    /**
     *指定子View是否可选择.true表示可选择，false表示不可选择
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void clearEditTextFocus(){
        groupIndex = -1;
        childIndex = -1;
        editId = -1;
    }
}
